package com.wentao.thisService.service.impl;


import com.wentao.thisFacade.common.UserInfo;
import com.wentao.thisService.service.CacheUserService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Auther: ZhaiYanTao
 * @Date: 2020/3/27 16:48
 * @Description: 缓存service实现
 */
@Service
@CacheConfig(cacheNames = "userInfoMap")
public class CacheUserServiceImpl implements CacheUserService {

    private Map<String, UserInfo> userInfoMap = new ConcurrentHashMap<>();

    @Override
    @Cacheable
    public Map<String, UserInfo> getAllUserInfoCache() {
        return userInfoMap;
    }

    @Override
    @CachePut(key = "{#userInfo.userName}")
    public Map<String, UserInfo> putToUserInfoCache(UserInfo userInfo) {
        userInfoMap.put(userInfo.getUserName(), userInfo);
        return userInfoMap;
    }

    @Override
    @CachePut(key = "{#userInfo.userName}")
    public Map<String, UserInfo> deleteUserInfoCache(UserInfo userInfo) {
        userInfoMap.remove(userInfo.getUserName());
        return userInfoMap;
    }

    @Override
    @CachePut
    public Map<String, UserInfo> clearUserInfoCache() {
        userInfoMap.clear();
        return userInfoMap;
    }


}
